package TplParser

import (
	"fmt"
	"gt.jtthink.com/Helper"
	"gt.jtthink.com/resource"
	"log"
	"os"
	"text/template"
)

type DBModelParser struct {
	TplContent string  //模板内容
}
//初始化 并读取模板内容
func NewDBModelParser() *DBModelParser  {
		return &DBModelParser{TplContent:Helper.UnGzip(resource.DB_TPL)}
}
func(this *DBModelParser) Parse(data interface{},target string){
	tpl:=template.New("DBModel").Funcs(Helper.NewTplFunction())
	tmpl, err:= tpl.Parse(this.TplContent)
	target = Helper.Ucfirst(Helper.CamelCase(target))
	fmt.Printf(target)
	if err!=nil{
		log.Fatal("db tpl parse-error:",err)
	}
	_,err =os.Stat(Helper.WorkDir+"/app/models/")
	if err != nil {
		//文件夹不存在则创建文件夹
		_ = os.Mkdir(Helper.WorkDir+"/app/models/",0755)
	}
	file,err:=os.OpenFile(Helper.WorkDir+"/app/models/"+target+".go",
		os.O_RDWR|os.O_CREATE|os.O_TRUNC,0666)
	if err!=nil{
		log.Fatal("model target error:",err)
	}
	err=tmpl.Execute(file,data)
	if err!=nil{
		log.Fatal("generate model error:",err)
	}
}